﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
using System.Reflection;
using Common;



namespace DAL
{
    public class Document<T>
    {
        //Add Info
        public bool AddDoc(T obj)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    en.AddObject(typeof(T).Name + "Set", obj);
                    en.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        //Edit Info
        public bool EditDoc(T obj)
        {
            using (CIMEntities en = new CIMEntities())
            {
                try
                {
                    string KeyName = typeof(T).Name.Replace("Info", "") + "Key";
                    int KeyValue = Convert.ToInt32(obj.GetType().GetProperty(KeyName).GetValue(obj, null));
                    string esql = " select value v from " + typeof(T).Name + "Set as v where v." + KeyName + "=@" + KeyName;
                    ObjectQuery<T> query = en.CreateQuery<T>(esql, new ObjectParameter(KeyName, KeyValue));
                    T DocObj = query.FirstOrDefault<T>();


                    Type DocObjType = DocObj.GetType();
                    PropertyInfo[] ps = obj.GetType().GetProperties();
                    foreach (PropertyInfo p in ps)
                    {
                        if (p.Name != "EntityKey" && p.Name != "EntityState" && p.Name != "CreateTime" && p.Name != "UserKey" && p.Name != KeyName)
                        {
                            DocObjType.GetProperty(p.Name).SetValue(DocObj, p.GetValue(obj, null), null);
                            //WebUtility.Response(p.Name + "=" + p.GetValue(obj, null).ToString()+"<br>");
                        }
                    }
                    en.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        //Select Single Info
        public T SelectDoc<T>(int DocKey)
        {
            using (CIMEntities en = new CIMEntities())
            {
                string KeyName = typeof(T).Name.Replace("Info", "") + "Key";
                string esql = "select value v from " + typeof(T).Name + "Set as v where v." + KeyName + "=@" + KeyName;

                ObjectQuery<T> query = en.CreateQuery<T>(esql, new ObjectParameter(KeyName, DocKey));
                return query.FirstOrDefault<T>();
            }
        }

        //Select Many Info
        public List<T> SelectDoc(string CustName, string Keyword)
        {
            using (CIMEntities en = new CIMEntities())
            {
                string esql = "select value v from "+typeof(T).Name + "Set as v";

                ObjectQuery<T> query = en.CreateQuery<T>(esql);

                return query.ToList<T>();
            }
        }

        //Delete Info
        public void DelDoc<T>(int DocKey)
        {
            using (CIMEntities en = new CIMEntities())
            {
                string KeyName = typeof(T).Name.Replace("Info", "") + "Key";
                object doc = en.GetObjectByKey(new EntityKey("CIMEntities." + typeof(T).Name+"Set", KeyName, DocKey));
                en.DeleteObject(doc);
                en.SaveChanges();
            }
        }


        //按用户查找
        public List<int> SelectDocAccountKey(int StartRowIndex, int MaxRows, string CustName, string Keyword)
        {   
            using (CIMEntities en = new CIMEntities())
            {
                var query = (from m in en.InfoServerSet select  m.MUserKey ).Distinct()
                           .Union((from m in en.InfoClientSet select m.MUserKey).Distinct())
                           .Union((from m in en.InfoDeviceSet select  m.MUserKey ).Distinct())
                           .Union((from m in en.InfoOfficeSet select  m.MUserKey ).Distinct())
                           .Union((from m in en.InfoSoftSet select  m.MUserKey ).Distinct());
                return query.OrderBy( m=>m).Skip(StartRowIndex).Take(MaxRows).ToList();
            }
        }

        public int SelectDocAccountCount(int StartRowIndex, int MaxRows, string CustName, string Keyword)
        {
            using (CIMEntities en = new CIMEntities())
            {
                var query = (from m in en.InfoServerSet select m.MUserKey).Distinct()
                           .Union((from m in en.InfoClientSet select m.MUserKey).Distinct())
                           .Union((from m in en.InfoDeviceSet select m.MUserKey).Distinct())
                           .Union((from m in en.InfoOfficeSet select m.MUserKey).Distinct())
                           .Union((from m in en.InfoSoftSet select m.MUserKey).Distinct());
                return query.Count();
            }
        }
    }

}
